package org.qpcr.community.project.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
import org.qpcr.community.project.model.entity.DataAnalysisDashboardChartsFilterConditionEntity;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
@RequiredArgsConstructor
public class DataAnalysisDashboardChartsFilterConditionService extends GenericReactiveCrudService<DataAnalysisDashboardChartsFilterConditionEntity, String> {

    public Mono<Void> saveBatch(List<DataAnalysisDashboardChartsFilterConditionEntity> entities) {
        if (entities == null || entities.isEmpty()) return Mono.empty();
        return super.insert(Flux.fromIterable(entities)).then();
    }

    public Mono<Map<String,List<DataAnalysisDashboardChartsFilterConditionEntity>>> getByGroupId(String groupId) {
        return createQuery()
            .is(DataAnalysisDashboardChartsFilterConditionEntity::getGroupId, groupId)
            .fetch()
            .collectList()
            .map(list -> list.stream()
                .collect(java.util.stream.Collectors.groupingBy(DataAnalysisDashboardChartsFilterConditionEntity::getChartId))
            );
    }

    public Map<String,List<DataAnalysisDashboardChartsFilterConditionEntity>> getByGroupIdSync(String groupId) {
        return getByGroupId(groupId).block();
    }

}
